package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
	private final ShoppingCartMapper shoppingCartMapper;
	
	private final DishMapper dishMapper;
	
	private final SetmealMapper setmealMapper;
	
	public ShoppingCartServiceImpl(ShoppingCartMapper shoppingCartMapper, DishMapper dishMapper, SetmealMapper setmealMapper) {
		this.shoppingCartMapper = shoppingCartMapper;
		this.dishMapper = dishMapper;
		this.setmealMapper = setmealMapper;
	}
	
	@Override
	public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
		//1.根据条件查询当前商品是否在购物车中
		ShoppingCart shoppingCart = new ShoppingCart();
		BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
		shoppingCart.setUserId(BaseContext.getCurrentId());
		shoppingCart = shoppingCartMapper.queryShoppingCar(shoppingCart);
		//2.如果已经存在，则更新购买的数量
		if (shoppingCart != null) {
			shoppingCart.setNumber(shoppingCart.getNumber() + 1);
			shoppingCartMapper.updateNumberById(shoppingCart);
		} else {
			shoppingCart = new ShoppingCart();
			//3.1 判断当前添加的是菜品还是套餐
			Long dishId = shoppingCartDTO.getDishId();
			if (dishId != null) {//如果是菜品
				//根据菜品id查询菜品
				Dish dish = dishMapper.getDishBy(null, null, null, dishId).remove(0);
				shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
				shoppingCart.setDishId(dishId);
				shoppingCart.setName(dish.getName());
				shoppingCart.setImage(dish.getImage());
				shoppingCart.setAmount(dish.getPrice());
				
			} else {//如果是套餐
				//根据套餐id查询套餐
				Setmeal setmeal = setmealMapper.getSetmealBy(null, null, null, shoppingCartDTO.getSetmealId()).remove(0);
				shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
				shoppingCart.setName(setmeal.getName());
				shoppingCart.setImage(setmeal.getImage());
				shoppingCart.setAmount(setmeal.getPrice());
			}
			//3.如果不存在，则插入数据，数量是1
			shoppingCart.setNumber(1);
			shoppingCart.setCreateTime(LocalDateTime.now());
			shoppingCart.setUserId(BaseContext.getCurrentId());
			shoppingCartMapper.insert(shoppingCart);
		}
	}
	
	@Override
	public Result getByUserId() {
		return Result.success(shoppingCartMapper.getByUserId(BaseContext.getCurrentId()));
	}
	
	@Override
	public Result deleteByDishOrSetmealById(ShoppingCartDTO shoppingCartDTO) {
		ShoppingCart shoppingCart = ShoppingCart.builder()
				.userId(BaseContext.getCurrentId())
				.build();
		BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
		
		List<ShoppingCart> shoppingCarts = shoppingCartMapper.getCart(shoppingCart);
		shoppingCarts.forEach(cart -> {
			if (cart.getNumber() == 1) {
				shoppingCartMapper.deleteByDishOrSetmealById(cart);
			} else {
				cart.setNumber(cart.getNumber() - 1);
				shoppingCartMapper.updateNumberById(cart);
			}
		});
		return Result.success();
	}
	
	@Override
	public Result clean() {
		shoppingCartMapper.cleanByUserId(BaseContext.getCurrentId());
		return Result.success();
	}
}
